package com.hq.door.dto;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

public class ImageCode {

	private String code;	//前台输入的验证码
	//sessionID,可设置(老师端、家长端、学生端)
	private static String sessionKey = "SESSIONCODE";
	private static int width = 120;  		//图片宽度
    private static int height = 30;  		//图片高度
    private static int codeLength = 4;  	//随机字符长度
    private static int fontSize = 20;		//字体大小  
    private static int fontStyle = 0;  		//字体样式,0:PLAIN,1:BOLD,2:ITALIC
    //字体名称
    private static String fontName = "Times New Roman";
    //默认随机字符串
    private static String randomString = "ABCDEFGHIJKLMNPQRSTUVWXYZ1234567890abcdefghijkmnpqrstuvwxyz";  
    //session名称
    private static int left = 20;
    private static int top = 25;
    
    /**
     * spmvc 注解模式
     * @param request	HTTPServletRequest
     * @return
     */
    public BufferedImage getImage(HttpServletRequest request){  
    	int offset = height<fontSize*2?(height-fontSize)/2:fontSize/2;
    	left = (width-fontSize*codeLength)/2;
    	top = height/2+fontSize/3;
        // 在内存中创建图象  
        BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);  
        // 获取图形上下文  
        Graphics g = image.getGraphics();  
        // 生成随机类  
        Random random = new Random();  
        // 设定背景色  
        g.setColor(getRandColor(200, 250));  
        g.fillRect(0, 0, width, height);  
        // 设定字体  
        g.setFont(new Font(fontName, fontStyle, fontSize));  
        g.setColor(getRandColor(160, 200));  
        String sRand = randomRand(codeLength);// 取随机产生的认证码  
        for (int i = 0; i < codeLength; i++) {  
            String rand = sRand.substring(i, i + 1);  
            // 将认证码显示到图象中  
            g.setColor(new Color(20 + random.nextInt(110), 20 + random  
                    .nextInt(110), 20 + random.nextInt(110)));// 调用函数出来的颜色相同，
            int zz = 0;
            if(offset>0){
            	zz=new Random().nextInt(offset);  
            	zz=zz%2==0?zz-offset:zz;  
            }
            g.drawString(rand, fontSize*i+left, top+zz);  
            //System.out.println("offset:"+offset+",zz:"+zz);
        }  
        for (int i = 0; i < height; i++) {  
            int x = random.nextInt(width);  
            int y = random.nextInt(height);  
            int xl = random.nextInt(height);  
            int yl = random.nextInt(height);  
            g.drawLine(x, y, x + xl, y + yl);  
        }
        request.getSession().setAttribute(sessionKey, sRand); 
        g.dispose();  
        return image;  
    }  
      
    /**
     * servlet 模式
     * @param request
     * @param width_
     * @param height_
     * @param fontSize_
     * @param codeLength_
     * @return
     */
    public static BufferedImage getImage(HttpServletRequest request,int width_,int height_,int fontSize_,int codeLength_){  
    	int offset = height_<fontSize_*2?(height_-fontSize_)/2:fontSize_/2;
    	left = (width_-fontSize_*codeLength_)/2;
    	top = height_/2+fontSize_/3;
        // 在内存中创建图象  
        BufferedImage image = new BufferedImage(width_, height_,BufferedImage.TYPE_INT_RGB);  
        // 获取图形上下文  
        Graphics g = image.getGraphics();  
        // 生成随机类  
        Random random = new Random();  
        // 设定背景色  
        g.setColor(getRandColor(200, 250));  
        g.fillRect(0, 0, width_, height_);  
        // 设定字体  
        g.setFont(new Font(fontName, fontStyle, fontSize_));  
        g.setColor(getRandColor(160, 200));  
        String sRand = randomRand(codeLength_);// 取随机产生的认证码  
        for (int i = 0; i < codeLength_; i++) {  
            String rand = sRand.substring(i, i + 1);  
            // 将认证码显示到图象中  
            g.setColor(new Color(20 + random.nextInt(110), 20 + random  
                    .nextInt(110), 20 + random.nextInt(110)));// 调用函数出来的颜色相同，
            int zz = 0;
            if(offset>0){
            	zz=new Random().nextInt(offset);  
            	zz=zz%2==0?zz-offset:zz;  
            }
            g.drawString(rand, fontSize_*i+left, top+zz);  
            //System.out.println("offset:"+offset+",zz:"+zz);
        }  
        for (int i = 0; i < height_; i++) {  
            int x = random.nextInt(width_);  
            int y = random.nextInt(height_);  
            int xl = random.nextInt(height_);  
            int yl = random.nextInt(height_);  
            g.drawLine(x, y, x + xl, y + yl);  
        }
        request.getSession().setAttribute(sessionKey, sRand); 
        g.dispose();  
        return image;  
    } 
      
    private static String randomResult(int length) {  
        String i[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" };  
        List<String> l = new ArrayList<String>();  
        l.addAll(Arrays.asList(i));  
        Random ran = new Random();  
        String s = "";  
        while (l.size() > 10 - length)  
            s += l.remove(ran.nextInt(l.size()));  
        s = s.replaceAll("^(0)(\\d)", "$2$1");  
        return s;  
    }  
  
    // 给定范围获得随机颜色  
    private static Color getRandColor(int fc, int bc) {
        Random random = new Random();  
        if (fc > 255)  
            fc = 255;  
        if (bc > 255)  
            bc = 255;  
        int r = fc + random.nextInt(bc - fc);  
        int g = fc + random.nextInt(bc - fc);  
        int b = fc + random.nextInt(bc - fc);  
        return new Color(r, g, b);  
    }  
  
    private static String randomRand(int n) {  
        String rand = "";  
        int len = randomString.length() - 1;  
        double r;  
        for (int i = 0; i < n; i++) {  
            r = (Math.random()) * len;  
            rand = rand + randomString.charAt((int) r);  
        }  
        return rand;  
    }  
  
	public String getSessionKey() {  
        return sessionKey;  
    }  
  
    public void setSessionKey(String sessionKey) {  
        this.sessionKey = sessionKey;  
    }

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getCodeLength() {
		return codeLength;
	}

	public void setCodeLength(int codeLength) {
		this.codeLength = codeLength;
	}

	public String getRandomString() {
		return randomString;
	}

	public void setRandomString(String randomString) {
		this.randomString = randomString;
	}

	public String getFontName() {
		return fontName;
	}

	public void setFontName(String fontName) {
		this.fontName = fontName;
	}

	public int getFontStyle() {
		return fontStyle;
	}

	public void setFontStyle(int fontStyle) {
		this.fontStyle = fontStyle;
	}

	public int getFontSize() {
		return fontSize;
	}

	public void setFontSize(int fontSize) {
		this.fontSize = fontSize;
	} 
	
}
